#include "../include/header.h"


struct ListNode_14 {
    BinaryTreeNode *m_pNode;
    ListNode_14 *m_pNext;
};



BinaryTreeNode* CreateTreeNode(int value)
{
    BinaryTreeNode* pNode = new BinaryTreeNode();
    pNode->m_nValue = value;
    pNode->m_pLeft = NULL;
    pNode->m_pRight = NULL;

    return pNode;
}

void ConnectTreeNodes(BinaryTreeNode* pParent, BinaryTreeNode* pLeft, BinaryTreeNode* pRight)
{
    if(pParent != NULL)
    {
        pParent->m_pLeft = pLeft;
        pParent->m_pRight = pRight;
    }
}

void PrintTreeNode(BinaryTreeNode* pNode)
{
    if(pNode != NULL)
    {
        printf("value of this node is: %d\n", pNode->m_nValue);

        if(pNode->m_pLeft != NULL)
            printf("value of its left child is: %d.\n", pNode->m_pLeft->m_nValue);
        else
            printf("left child is null.\n");

        if(pNode->m_pRight != NULL)
            printf("value of its right child is: %d.\n", pNode->m_pRight->m_nValue);
        else
            printf("right child is null.\n");
    }
    else
    {
        printf("this node is null.\n");
    }

    printf("\n");
}

void PrintTree(BinaryTreeNode* pRoot)
{
    PrintTreeNode(pRoot);

    if(pRoot != NULL)
    {
        if(pRoot->m_pLeft != NULL)
            PrintTree(pRoot->m_pLeft);

        if(pRoot->m_pRight != NULL)
            PrintTree(pRoot->m_pRight);
    }
}

void DestroyTree(BinaryTreeNode* pRoot)
{
    if(pRoot != NULL)
    {
        BinaryTreeNode* pLeft = pRoot->m_pLeft;
        BinaryTreeNode* pRight = pRoot->m_pRight;

        delete pRoot;
        pRoot = NULL;

        DestroyTree(pLeft);
        DestroyTree(pRight);
    }
}

void PrintTreeByLevel(BinaryTreeNode *root)
{
    if (NULL == root)
        return ;

    ListNode_14 *head, *rear, *temp;
    head = (ListNode_14*)malloc(sizeof(ListNode_14));

    head->m_pNode = root;
    head->m_pNext = NULL;
    rear = head;


    while (NULL!= head) {
        if (head->m_pNode->m_pLeft) {
            temp = (ListNode_14*)malloc(sizeof(ListNode_14));
            temp->m_pNode = head->m_pNode->m_pLeft;
            temp->m_pNext = NULL;
            rear->m_pNext = temp;
            rear = temp;
        }

        temp = NULL;

        if (NULL != head->m_pNode->m_pRight) {
            temp = (ListNode_14*)malloc(sizeof(ListNode_14));
            temp->m_pNode = head->m_pNode->m_pRight;
            temp->m_pNext = NULL;
            rear->m_pNext = temp;
            rear = temp;
        }

        cout << head->m_pNode->m_nValue <<" " ;
        temp = head;
        head = head->m_pNext;
        free(temp);
    }
    cout << endl;
}


//            1
//          /  \
//         2    3
//             /  \
//            4    5
//           / \    \
//         6    7    8
//                     \
//                      9
//                        \
//                         10

void TestForForteen()
{
    BinaryTreeNode* node1 = CreateTreeNode(1);
    BinaryTreeNode* node2 = CreateTreeNode(2);
    BinaryTreeNode* node3 = CreateTreeNode(3);
    BinaryTreeNode* node4 = CreateTreeNode(4);
    BinaryTreeNode* node5 = CreateTreeNode(5);
    BinaryTreeNode* node6 = CreateTreeNode(6);
    BinaryTreeNode* node7 = CreateTreeNode(7);
    BinaryTreeNode* node8 = CreateTreeNode(8);
    BinaryTreeNode* node9 = CreateTreeNode(9);
    BinaryTreeNode* node10 = CreateTreeNode(10);

    ConnectTreeNodes(node1, node2, node3);
    ConnectTreeNodes(node3, node4, node5);
    ConnectTreeNodes(node4, node6, node7);
    ConnectTreeNodes(node5, NULL, node8);

    ConnectTreeNodes(node8, NULL, node9);
    ConnectTreeNodes(node9, NULL, node10);

    PrintTreeByLevel(node1);
}


